package awesome.cpu.structure;

public class Mux2WR1 extends NonSequentialComponent {
	
	public final static int KEY_REG_DST = 1;
	private int mRegDst;
	
	public final static int KEY_RS2 = 2;
	private int mRs2;
	public final static int KEY_RD1 = 3;
	private int mRd1;
	private final int mLRAddress = 30;
	
	public final static int KEY_SELECTED = 4;
	//private int mSelected;
	
	private final static Mux2WR1 mInstance = new Mux2WR1();
	
	private Mux2WR1() {}
	
	public static Mux2WR1 getInstance() {
		return mInstance;
	}
	
	@Override
	boolean setData(int keyInterface, int data) {
		switch (keyInterface) {
		case KEY_REG_DST:
			mRegDst = data;
			return true;
		case KEY_RS2:
			mRs2 = data;
			return true;
		case KEY_RD1:
			mRd1 = data;
			return true;
		}
		return false;
	}

	@Override
	int getData(int keyInterface) {
		
		BusMonitor.transferData(Control.getInstance(), Control.KEY_REG_DST,
				this, KEY_REG_DST);
		BusMonitor.transferData(InstructionRegister.getInstance(), 
				InstructionRegister.KEY_INSTRUCTION_4_0,
				this, KEY_RS2);
		BusMonitor.transferData(InstructionRegister.getInstance(), 
				InstructionRegister.KEY_INSTRUCTION_18_14,
				this, KEY_RD1);
		
		switch (keyInterface) {
		case KEY_SELECTED:
			return select();
		}
		return 0;
	}
	
	private int select() {
		switch (mRegDst) {
		case 0:
			return mRs2;
		case 1:
			return mRd1;
		case 2:
			return mLRAddress;
		}
		return 0;
	}
}
